package best.driver.android.Utils;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import best.driver.android.AppInfo;
import best.driver.android.BestApp;
import best.driver.android.Utils.actions.Action1;

//import kr.cbh.android.customer.util.actions.Action1;

;

public class SmsSender {
    private static String SMS_SEND_ACTIOIN = "kr.cbh.android.customer.SMS_SEND_ACTIOIN";
    private static String SMS_DELIVERED_ACTION = "kr.cbh.android.customer.SMS_DELIVERED_ACTION";

    private static BroadcastReceiver smsReceiver;

    private static Action1<Integer> callback;

    public static void send(Context context, String number, String content,final Action1<Integer> aCallback) {

        callback = aCallback;

        Intent itSend = new Intent(SMS_SEND_ACTIOIN);
        Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);

        PendingIntent mSendPI = PendingIntent.getBroadcast(BestApp.getInstance(), 0, itSend, 0);
        PendingIntent mDeliverPI = PendingIntent.getBroadcast(BestApp.getInstance(), 0, itDeliver, 0);

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(number, null, content, mSendPI, mDeliverPI);

        regReceiver(context);
    }

    public static void release(Context context) {
        if (smsReceiver != null) {
            try {
                context.unregisterReceiver(smsReceiver);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void regReceiver(final Context context) {
        if (smsReceiver == null) {
            smsReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals(SMS_SEND_ACTIOIN)) {
                        try {
                            switch (getResultCode()) {
                            case Activity.RESULT_OK:
                                /* 发送短信成功 */
                                AppInfo.showToast(context, "短信发送成功");
                                callback.run(1);
                                break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                /* 发送短信失败 */
                                AppInfo.showToast(context, "短信发送失败");
                                callback.run(0);
                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:
                                AppInfo.showToast(context, "短信发送失败");
                                callback.run(0);
                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:
                                AppInfo.showToast(context, "短信发送失败");
                                callback.run(0);
                                break;
                            default:
                                AppInfo.showToast(context, "短信发送失败");
                                callback.run(0);
                                break;
                            }
                        } catch (Exception e) {
                            e.getStackTrace();
                        }
                    }
                }
            };
        }

        IntentFilter filter = new IntentFilter(SMS_SEND_ACTIOIN);
        context.registerReceiver(smsReceiver, filter);

        filter = new IntentFilter(SMS_DELIVERED_ACTION);
        context.registerReceiver(smsReceiver, filter);
    }
}
